package cn.source.zk.service.impl;

import cn.source.common.exception.ServiceException;
import cn.source.common.utils.StringUtils;
import cn.source.common.utils.bean.BeanValidators;
import cn.source.zk.domain.dto.SettingQueryFieldsDTO;
import cn.source.zk.domain.dto.TableDefinitionDTO;
import cn.source.zk.domain.entity.ZkSettingQueryFields;
import cn.source.zk.domain.entity.ZkTableDefinition;
import cn.source.zk.domain.vo.TableDefinitionVO;
import cn.source.zk.domain.vo.ZkSettingQueryFieldsVO;
import cn.source.zk.mapper.ZkSettingQueryFieldsMapper;
import cn.source.zk.mapper.ZkTableDefinitionMapper;
import cn.source.zk.service.ZkSettingQueryFieldsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author shiqi
 * @create 2025-04-18 14:21
 */
@Service
public class ZkSettingQueryFieldsServiceImpl implements ZkSettingQueryFieldsService {

    @Autowired
     private ZkSettingQueryFieldsMapper zkSettingQueryFieldsMapper;

    @Autowired
    private ZkTableDefinitionMapper zkTableDefinitionMapper;

    @Autowired
    private Validator validator;

    /**
     * 查询数据列表
     * @param dto
     * @return
     */
    @Override
    public List<ZkSettingQueryFieldsVO> listPage(SettingQueryFieldsDTO dto) {

        return zkSettingQueryFieldsMapper.listPage(dto);

    }

    /**
     * 新增查询字段
     * @param zkSettingQueryFields
     */
    @Override
    public void addSettingQueryFields(ZkSettingQueryFields zkSettingQueryFields) {
        zkSettingQueryFieldsMapper.add(zkSettingQueryFields);
    }

    /**
     * 编辑查询字段
     * @param zkSettingQueryFields
     */
    @Override
    public void editSettingQueryFields(ZkSettingQueryFields zkSettingQueryFields) {
        zkSettingQueryFieldsMapper.edit(zkSettingQueryFields);
    }

    /**
     * 根据id查询字段查询
     * @param id
     * @return
     */
    @Override
    public ZkSettingQueryFields getSettingQueryFieldsById(Integer id) {
        return zkSettingQueryFieldsMapper.getSettingQueryFieldsById(id);
    }


    /**
     * 删除查询字段
     * @param id
     */
    @Override
    public void deleteSettingQueryFields(Integer id) {
        zkSettingQueryFieldsMapper.del(id);
    }

    @Override
    public String importUser(List<ZkSettingQueryFields> userList, boolean updateSupport, String operName) {

        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZkSettingQueryFields user : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, user);
                // user.setCreateBy(operName);
                user.setCreator(operName);
                user.setCreateTime(LocalDateTime.now());
                user.setModifier(operName);
                user.setUpdateTime(LocalDateTime.now());

                // user.setFieldName(operName);
                this.addSettingQueryFields(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、字段 " + user.getFieldName() + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、字段 " + user.getFieldName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


    /**
     * 新增表标识
     * @param tableDefinition
     */
    @Override
    public void addTableDefinition(ZkTableDefinition tableDefinition) {
        zkTableDefinitionMapper.add(tableDefinition);
    }

    /**
     * 修改表标识
     * @param tableDefinition
     */
    @Override
    public void editTableDefinition(ZkTableDefinition tableDefinition) {
        zkTableDefinitionMapper.edit(tableDefinition);
    }

    /**
     * 根据id查询表标识
     * @param id
     * @return
     */
    @Override
    public ZkTableDefinition getTableDefinitionById(Integer id) {
        return zkTableDefinitionMapper.getTableDefinitionById(id);
    }

    /**
     * 删除表标识
     * @param id
     */
    @Override
    public void deleteTableDefinition(Integer id) {
        zkTableDefinitionMapper.del(id);
    }


    /**
     * 查询表标识分页列表
     * @param dto
     * @return
     */
    @Override
    public List<TableDefinitionVO> tableDefinitionListPage(TableDefinitionDTO dto) {
        List<TableDefinitionVO> zkTableDefinitions = zkTableDefinitionMapper.listPage(dto);
        return zkTableDefinitions;
    }

    /**
     * 获取谐波电流数据
     * @return
     */
    @Override
    public List<ZkSettingQueryFieldsVO> getIhrData(SettingQueryFieldsDTO dto) {
        return zkSettingQueryFieldsMapper.getIhrData(dto);
    }

}
